/*******************************************************************************

This code file produces Table A9, "Incidence Analysis of 421-a: Actual Versus Incidence-Minimizing Counterfactual," and Figure A18, "Developer Profit Incidence Share by Neighborhood Tabulation Area."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
*** Load Stata-TeX

	do "$code/modules/stata-tex.do"
	cd "$tables/estimates"
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
		
	local n_boot = 200
	
********************************************************************************
* Complete table analysis
********************************************************************************
	
*** Bootstrap actual incidence share

	capture program drop boot_incidence
	program define boot_incidence, rclass
	
		preserve
					
		logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta) asis
			
		mat b = e(b)
		local coef_dtaxrate = b[1,1]
		predict xb, xb
		
		gen dlprofit = (1 / `coef_dtaxrate') * log(1 + exp(xb))
		gen mvalue = (assesstot-assessland)/(frac_assess*underassess)
							  
		summ dlprofit if inclusionary_onsite == 1 [aw = mvalue]
		local dlprofit = r(mean)
		
		summ dtaxrate_onsite if inclusionary_onsite == 1 [aw = mvalue]
		local dtaxrate_onsite = r(mean)
		
		local incidence = `dlprofit' / `dtaxrate_onsite'
		return scalar tmp = `incidence'
		
		restore
		
	end
	
	xtset,clear
	bootstrap tmp=r(tmp), reps(`n_boot') seed(1234) cluster(nta) idcluster(nta_boot): boot_incidence
	
	matrix mat_incidence = r(table)
	local incidence_estimate = mat_incidence[1,1]
	local incidence_stderr = mat_incidence[2,1]
	
	local incidence_obs = e(N)
	local incidence_clust = e(N_clust)
	
	insert_into_file using tableA9.csv, key(incidence_estimate) value(`incidence_estimate') format(%12.3f)
	insert_into_file using tableA9.csv, key(incidence_stderr) value(`incidence_stderr') format(%12.3f)
	insert_into_file using tableA9.csv, key(incidence_obs) value(`incidence_obs') format(%12.0fc)
	insert_into_file using tableA9.csv, key(incidence_clust) value(`incidence_clust') format(%12.0fc)
	
*** COUNTERFACTUAL w/ dtaxrate_onsite proportional to breakeven in p.p.

	capture program drop boot_incidence_cf
	program define boot_incidence_cf, rclass
	
		preserve
		
		drop if borough == 5
				 		  
		logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta) asis
			
		mat b = e(b)
		local coef_dtaxrate = b[1,1]
		predict xb, xb

		gen mvalue = (assesstot-assessland)/(frac_assess*underassess)
		summ inclusionary_onsite [aw = mvalue]
		local logodds = log(r(mean)/(1-r(mean)))
		
		gen dlprofit_cf = (1 / `coef_dtaxrate') * log(1 + exp(`logodds'))
		
		summ dlprofit_cf [aw = mvalue]
		local dlprofit_cf = r(mean)
		
		summ dtaxrate_onsite [aw = mvalue]
		local dtaxrate_onsite_cf = r(mean)
		
		local incidence_cf = `dlprofit_cf' / `dtaxrate_onsite_cf'
		return scalar tmp = `incidence_cf'
		
		restore
		
	end
	
	xtset,clear
	bootstrap tmp=r(tmp), reps(`n_boot') seed(1234) cluster(nta)  idcluster(nta_boot): boot_incidence_cf
	
	matrix mat_incidence_cf = r(table)
	local incidence_cf_estimate = mat_incidence_cf[1,1]
	local incidence_cf_stderr = mat_incidence_cf[2,1]
	
	local incidence_cf_obs = e(N)
	local incidence_cf_clust = e(N_clust)
	
	insert_into_file using tableA9.csv, key(incidence_cf_estimate) value(`incidence_cf_estimate') format(%12.3f)
	insert_into_file using tableA9.csv, key(incidence_cf_stderr) value(`incidence_cf_stderr') format(%12.3f)
	insert_into_file using tableA9.csv, key(incidence_cf_obs) value(`incidence_cf_obs') format(%12.0fc)
	insert_into_file using tableA9.csv, key(incidence_cf_clust) value(`incidence_cf_clust') format(%12.0fc)
	
*** Incidence by NTA

	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta)
		
	mat b = e(b)
	local coef_dtaxrate = b[1,1]
	predict xb, xb
	
	gen dlprofit = (1 / `coef_dtaxrate') * log(1 + exp(xb))
	gen mvalue = (assesstot-assessland)/(frac_assess*underassess)

	collapse dlprofit dtaxrate_onsite [aw=mval], by(nta nta_code)
	
	gen incidence = dlp/dtax
	replace incidence = 1 if incidence > 1 & !missing(incidence)

	merge 1:1 nta_code using "$data/GIS_boundaries/neighborhood_boundaries/df.dta", nogen
	
	format incidence %6.2f
	
	spmap incidence using "$data/GIS_boundaries/neighborhood_boundaries/coords.dta", ///
		id(_ID) fcolor(Greys2) ndf(white) legend(position(4)) ///
		osize(vthin vthin vthin vthin vthin) ndsize(vthin)
		
	graph export "$figs/incidence_nta.pdf", replace
	graph export "$figs_overleaf/incidence_nta.pdf", replace
	

/*
*** Savings from reform

	capture program drop boot_reform
	program define boot_reform, rclass
	
		preserve
		
		drop if yearpermit == 2015 | borough == 5
		
		gen tax_savings = dtaxrate_onsite*assesstot/sh_taxable

		summ tax_savings if inclusionary_onsite == 1
		local tax_savings = r(sum)
		
		logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta) asis
			
		mat b = e(b)
		local coef_dtaxrate = b[1,1]
		predict xb, xb

		summ inclusionary_onsite [aw = mvalue]
		local logodds = log(r(mean)/(1-r(mean)))
		local pr_takeup = r(mean)
		
		gen dtaxrate_reform = (`logodds' - xb) / `coef_dtaxrate'
		gen tax_savings_reform = dtaxrate_reform * assesstot / sh_taxable
		
		summ tax_savings_reform
		local tax_savings_cf = `pr_takeup' * r(sum)
		
		local reform_savings = 1 - `tax_savings_cf'/`tax_savings'
		return scalar tmp = `reform_savings'
		
		restore
		
	end

	local n_boot = 100
	bootstrap tmp=r(tmp), reps(`n_boot') cluster(nta) idcluster(nta_boot): boot_reform

	*/
	
********************************************************************************
* Create table
********************************************************************************
	
	cat tableA9.csv

	cap erase "$tables/tableA9.tex"

	cd "$code/modules"
	
	 table_from_tpl, t("$tables/templates/tableA9.tex") ///
						r("$tables/estimates/tableA9.csv") ///
						o("$tables/output/tableA9.tex") 
						
	capture table_from_tpl, t("$tables/templates/tableA9.tex") ///
						r("$tables/estimates/tableA9.csv") ///
						o("$tables_overleaf/tableA9.tex")
	
	
